//
//  ZLPlusToolbar.swift
//  新浪微博
//
//  Created by 朱玉龙 on 16/10/6.
//  Copyright © 2016年 朱玉龙. All rights reserved.
//

import UIKit
import pop

let btnH: CGFloat = 110
let btnW: CGFloat = 80
class ZLPlusToolbar: UIToolbar {
    
    var targetController:UIViewController?
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func show(controller: UIViewController) {
        
        controller.view.addSubview(self)
        targetController = controller
    }
    
    private func setupUI() {
        
        let image = UIImage(named: "compose_slogan")
        let imageView = UIImageView(image: image)
        
        addSubview(imageView)
        imageView.snp.makeConstraints { (make) in
            
            make.top.equalTo(self).offset(150)
            make.centerX.equalTo(self)
        }
        
        addBtns()
    }
    
    let margin = (UIScreen.main.bounds.size.width - btnW * 3) / (3 + 1)
    var btns = [UIButton]()
    private func addBtns() {
        
        for i in 0..<6 {
            let button = ZLPlusButton(title: "\(i)", titleColor: UIColor.darkGray, fontSize: 15)
            let image = #imageLiteral(resourceName: "tabbar_compose_lbs")
            button.setImage(image, for: .normal)
            button.tag = i
            
            let btnX = margin + (margin + btnW) * CGFloat(i % 3)
            let btnY = (margin + btnH) * CGFloat(i / 3) + screenHeight
            button.frame = CGRect(x: btnX, y: btnY, width: btnW, height: btnH)
            
            addSubview(button)
            button.addTarget(self, action: #selector(buttonClick(btn:)), for: .touchUpInside)
            btns.append(button)
        }
        
    }
    //按钮点击
    @objc private func buttonClick(btn: UIButton) {
        
        for button in btns {
            
            button.alpha = 0.2
            
            if btn.tag == button.tag {
                
                
                UIView.animate(withDuration: 0.5, animations: {
                    
                    button.transform = CGAffineTransform.init(scaleX: 2, y: 2)
                    
                }){ (_) in
                    
                    let string = Bundle.main.infoDictionary!["CFBundleName"] as! String + "." + "ZLPlusViewController"

                    let type = NSClassFromString(string) as! UIViewController.Type
                    
                    let vc = type.init()
                    
                    //                   let vc = ZLPlusViewController()
                    let nv = UINavigationController(rootViewController: vc)
                    self.targetController?.present(nv, animated: true, completion: {
                        
                        self.removeFromSuperview()
                    })
                }
            }
            else {
                
                UIView.animate(withDuration: 0.5, animations: {
                    button.transform = CGAffineTransform.init(scaleX: 0.1, y: 0.1)
                })
            }
            
        }
        
        
    }
    //将要移动到父视图
    override func willMove(toSuperview newSuperview: UIView?) {
        super.willMove(toSuperview: newSuperview)
        
        for value in btns.enumerated() {
            
            buttonAnimation(btn: value.element, offset: value.offset)
            
        }
        
    }
    //弹出效果
    private func buttonAnimation(btn: UIButton, offset: Int,isUp: Bool? = true) {
        
        let anim = POPSpringAnimation(propertyNamed: kPOPViewCenter)
        anim?.springBounciness = 10
        anim?.springSpeed = 12
        
        let y = isUp == true ? btn.center.y - 350 : btn.center.y + 350
        anim?.beginTime = CACurrentMediaTime() + 0.025 * Double(offset)
        anim?.toValue = NSValue.init(cgPoint: CGPoint(x: btn.center.x, y: y))
        
        btn.pop_add(anim, forKey: nil)
    }
    //触摸事件
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        
        for value in btns.reversed().enumerated() {
            
            buttonAnimation(btn: value.element, offset: value.offset, isUp: false)
            
        }
        
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.5) {
            
            self.removeFromSuperview()
        }
    }
    
}



